import React, {Component} from 'react';
import storageUtil from "../../../../utils/storageUtil";
import {Avatar, Button, List, message, Modal} from "antd";

class DealOrders extends Component {

    state = {
        question: '',
        visible:false,
        confirmLoading:false,
        modalText:'请输入回复'
    }

    componentDidMount() {
        const oid = storageUtil.getOrder().oid
        this.$myAxios.get('/question/showUndoQuestion/'+oid).then(res => {
            console.log(res.data)
            this.setState({
                question: res.data
            })
        })

    }

    render() {

        const handleOk = () => {
            this.setState({modalText:'输入回复'})
            this.setState({confirmLoading:true})
            const answer = this.input.value
            if(answer){
                const a={
                    'qid': storageUtil.getQuestion().qid,
                    'uid': storageUtil.getServicer().uid,
                    'answer': answer
                }
                console.log(a)
                this.$myAxios.post("/answer/addAnswer",a).then(res => {
                    if(res.data){
                        message.success("回复成功")
                        window.location.reload()
                    }
                })
            }
            else
                message.error('回复不能为空')
            setTimeout(() => {
                this.setState({visible:false})
                this.setState({confirmLoading:false})
            }, 2000);
        };

        const handleCancel = () => {
            console.log('Clicked cancel button');
            this.setState({visible:false})
        };

        return (
            <List
                itemLayout="horizontal"
                dataSource={this.state.question}
                renderItem={item => (
                    <List.Item>
                        <List.Item.Meta
                            avatar={<Avatar src="/static/image/order.png"/>}
                            title={item.question}
                            description={"创建时间：" + item.createtime}
                        />
                        <Button type="primary" onClick={ () =>
                        {
                            this.setState({visible:true})
                            storageUtil.saveQuestion(item)
                            console.log(item)
                        }
                        }>
                            回复
                        </Button>
                        <Modal
                            title="回复工单"
                            visible={this.state.visible}
                            okText={'确认'}
                            cancelText={'取消'}
                            onOk={handleOk}
                            confirmLoading={this.state.confirmLoading}
                            onCancel={handleCancel}
                        >
                            <p>{this.state.modalText}
                                <input
                                    ref={input => this.input = input}
                                />
                            </p>
                        </Modal>

                    </List.Item>

                )}

            />
        );
    }
}

export default DealOrders;